//
//  YKDoclistController.m
//  HMPersonalDoctor
//
//  Created by 叶文凯 on 16/9/8.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "YKDoclistController.h"
#import "YKDoctorModel.h"
#import "YKCategoryCell.h"
#import "YKDoctlistCell.h"
#import "YKHeaderView.h"
#import "YKCategoryDetailController.h"
#import "InfoOfDoctorController.h"
#import "YKWebViewController.h"
#import "YZAskViewController.h"

static NSString *categoryCellID = @"category";
static NSString *doclistCellID = @"doclist";
static NSString *cellID = @"cell";
@interface YKDoclistController ()<UITableViewDataSource,UITableViewDelegate,YKCategoryCellDelegate,YKDoctlistCellDelegate>

@end

@implementation YKDoclistController
{
    NSArray *_doctList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"名医快诊";
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
    item.title = @"返回";
    self.navigationItem.backBarButtonItem = item;
    
    
    [self loadData];
    
    [self setupUI];
    
}

- (void)setupUI
{
    //    左侧目录按钮
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"首页"  style:UIBarButtonItemStyleDone target:self action:@selector(leftItemAction)];
//    
//    self.navigationItem.leftBarButtonItem = leftItem;
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    //    注册自定义cell
    UINib *nib = [UINib nibWithNibName:@"YKDoctlistCell" bundle:nil];
    
    [tableView registerNib:nib forCellReuseIdentifier:doclistCellID];
    
    [tableView registerClass:[YKCategoryCell class] forCellReuseIdentifier:categoryCellID];
    
    [self.view addSubview:tableView];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
//    设置组头轮播器
    
    YKHeaderView *headerView = [[YKHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 180)];
    
    tableView.tableHeaderView = headerView ;
#pragma mark - 设置组尾字体居中 未实现
//   设置组尾
    UIButton *button = [[UIButton alloc] init];
    
    [button setTitle:@"下拉加载更多" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button sizeToFit];
//    设置文字居中
    tableView.tableFooterView = button;
    
    
#pragma mark - 取消cell的分割线
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    
}


#pragma mark  - 实现目录按钮的功能

- (void)leftItemAction
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger num;
    switch (section) {
        case 0:
            num = 1;
            break;
           
        case 1:
            num = _doctList.count;
            break;
            
        default:
            break;
    }
    
    return num;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
    
    YKCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:categoryCellID forIndexPath:indexPath ];
        tableView.rowHeight = 160;
    
        cell.delgate = self;
        
    return cell;
} else  {
    
    YKDoctlistCell *cell = [tableView dequeueReusableCellWithIdentifier:doclistCellID forIndexPath:indexPath];
    
    cell.model = _doctList[indexPath.row];
    tableView.rowHeight = 143;
    cell.delegate = self;
    
    return cell;
}
    
}
//设置组头文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = nil;
    if (section == 1) {
        
        title = @"名医推荐";
    }
    return title;
}

//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//{
//    NSString *title = nil;
//    if (section == 1) {
//        
//        title = @"下拉加载更多";
//        
//    }
//    
//    return title;
//}

// 加载数据源
- (void)loadData
{
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"FamDoctor.plist" ofType:nil];
    
    NSArray *docList = [NSArray arrayWithContentsOfFile:filePath];
    
    NSMutableArray *tempDoct = [NSMutableArray arrayWithCapacity:docList.count];
    for (NSDictionary *dict in docList) {
        
        YKDoctorModel *model = [YKDoctorModel doctorModelWithDict:dict];
        
        [tempDoct addObject:model];
        
    }
    
    _doctList = tempDoct.copy;
    
}

#pragma mark - 设置代理方法

#pragma mark - 实现选中cell不变色

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    InfoOfDoctorController *infoController = [[InfoOfDoctorController alloc] init];
    
    infoController.model = _doctList[indexPath.row];
    
    [self.navigationController pushViewController:infoController animated:YES];
  
}

#pragma mark - 实现分类cell按钮的控制器跳转

- (void)pushCategoryController:(NSString *)title
{
    NSString *newTitle = [title stringByReplacingOccurrencesOfString:@"\U0000fffc\n\n" withString:@""] ;
    
    YKCategoryDetailController *categoryDetailController = [[YKCategoryDetailController alloc] init];
    
    categoryDetailController.name = newTitle;
    categoryDetailController.modelList = _doctList;
    
    [self.navigationController pushViewController:categoryDetailController animated:YES];

}

- (void)loadWebViewController:(NSInteger)tag
{
    
    YKWebViewController *webController = [[YKWebViewController alloc] init];
    
    webController.tag = tag;
    
    [self.navigationController pushViewController:webController animated:YES];
    
}

#pragma mark - 实现doclistCell 的代理方法

- (void)askQuestion:(UIImage *)image
{
    YZAskViewController *askVC = [[YZAskViewController alloc] init];
    askVC.image = image;
    
    [self.navigationController pushViewController:askVC animated:YES];
    
    
}

- (void)requireOtherHospital:(UIButton *)sender
{
    YKWebViewController *webController = [[YKWebViewController alloc] init];
    webController.tag = sender.tag;
    
    [self.navigationController pushViewController:webController animated:YES];
    
    
}


@end
